<?php

declare(strict_types=1);

namespace app\api\controller\v1;

use app\api\BaseApiController;
use app\apps\model\Apps;

class Get extends BaseApiController
{
    /**
     * 根据短ID读取小程序应用的接口地址
     */
    public function index()
    {
        $one = Apps::with([
            'versions' => function ($query) {
                $query->order('id', 'desc');
                $query->limit(1);
            },
        ])->where('tag', $this->data['name'])->find();
        if ($one && !empty($one->versions)) {
            $version = $one->versions[0];
            $api = $version->api;
            if (strpos($api, 'http') === false) {
                $api = DOMAIN . $api;
            }
            return json(['api' => $api]);
        }
        return json(['error' => '应用不存在'], 401);
    }
}
